/**
 * @file SignPage.as

 *  1、修改文案：
 A）原“签名板”标题，改为“嘉宾留影区”及“嘉宾签名区”。
 B）右侧按钮“笔触”改为“字体”；“重签”改为“修改”。
 2、嘉宾留影区：等比例放大，放大后尺寸为643*541，与嘉宾签名区垂直居中对齐。
 * @author db0@qq.com
 * @version 1.0.1
 * @update date 2018-01-15
 */
package yonghe
{
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;

	public class SignPage extends Sprite
	{
		private static var _main:SignPage = null;
		public static function get main():SignPage
		{
			if(_main==null)
				_main = new SignPage();
			return _main;
		}
		private static var bichuBtns:Sprite;
		private var bichu1:MyBtn;
		private var bichu2:MyBtn;
		private var bichu3:MyBtn;
		private function showBichu(e:Event=null):void
		{
			if(bichuBtns==null)
			{
				bichuBtns = new Sprite();
				bichuBtns.x = 1233;
				if(test.isVert)
					bichuBtns.x = 415;

				bichuBtns.y = bichuBtn.y;

				bichu1 = new MyBtn(changeBichu1,Assets.bichu1_png_url);
				bichu2 = new MyBtn(changeBichu2,Assets.bichu2_png_url);
				bichu3 = new MyBtn(changeBichu3,Assets.bichu3_png_url);

				bichuBtns.addChild(bichu1);
				bichuBtns.addChild(bichu2);
				bichuBtns.addChild(bichu3);
				bichu2.x = 150;
				bichu3.x = 300;

			}
			addChild(bichuBtns);
			bichuBtns.visible = true;
		}
		private function changeBichu1():void
		{
			hideBichu();
			drawBoard.lineSize = 5;

		}
		private function changeBichu2():void
		{
			hideBichu();
			drawBoard.lineSize = 10;
		}
		private function changeBichu3():void
		{
			hideBichu();
			drawBoard.lineSize = 15;
		}
		private static function hideBichu():void
		{
			if(bichuBtns)
				bichuBtns.visible = false;
		}

		private static function get titleString():String
		{
			if(Datas.signTitle)
				return Datas.signTitle;
			return "";
			//return "签名板";
		}

		public function SignPage()
		{
			_main = this;
			addChild(new Bitmap(test.main.bgBmpd));
			if(!test.isVert)
			{
				if(!Datas.autoQuit)
				{
					var title:TextField = ViewSet.makeTxtLine(titleString,33/818*width,0xab3736);
					title.x = (width-title.height)/2;
					title.y = 140;
					addChild(title);
				}else{
					//218,186,400,81
					var title1:TextField = ViewSet.makeTxtLine("嘉宾留影区",70,0xab3736);
					title1.x = 218;
					title1.y = 186;
					addChild(title1);

					//1031,186,403,87
					var title2:TextField = ViewSet.makeTxtLine("嘉宾签名区",70,0xab3736);
					title2.x = 1031;
					title2.y = title1.y;
					addChild(title2);
				}
			}
			//rePhotoBtn = new MyBtn(toPhoto,Assets.newBtn("重拍"));
			rePhotoBtn = new MyBtn(toPhoto,Assets.rephoto_png_url);
			photoBtn = new MyBtn(toPhoto,Assets.takephoto_png_url);
			if(Datas.autoQuit){
				bichuBtn = new MyBtn(showBichu,Assets.zit_png_url);
			}else{
				bichuBtn = new MyBtn(showBichu,Assets.bichu_png_url);
			}
			saveBtn = new MyBtn(toNext,Assets.upload_png_url);
			if(Datas.autoQuit){
				resetBtn = new MyBtn(toClear,Assets.xiugg1_png_url);
			}else{
				resetBtn = new MyBtn(toClear,Assets.resign_png_url);
			}
			backBtn = new MyBtn(quit,Assets.back_png_url);

			if(!Datas.autoQuit)
				addChild(rePhotoBtn);
			addChild(photoBtn);
			if(Datas.hasSignBoard)
			{
				addChild(resetBtn);
				addChild(bichuBtn);
			}

			addChild(saveBtn);

			if(Datas.autoQuit)
				addChild(backBtn);

			if(test.isVert){
				saveBtn.x = 
					resetBtn.x = 
					bichuBtn.x = 
					backBtn.x = 
					photoBtn.x = 900;

				photoBtn.y = 980;
				bichuBtn.y = 1080;
				resetBtn.y = 1180;
				saveBtn.y = 1330;
				backBtn.y = 1430;

				rePhotoBtn.x = photoBtn.x;
				rePhotoBtn.y = photoBtn.y;
			}else{
				saveBtn.x = 
					resetBtn.x = 
					bichuBtn.x = 
					backBtn.x = 
					photoBtn.x = 1700;

				photoBtn.y = 300;
				bichuBtn.y = 400;
				resetBtn.y = 500;
				saveBtn.y = 650;
				backBtn.y = 750;

				rePhotoBtn.x = photoBtn.x;
				rePhotoBtn.y = photoBtn.y;
			}

			drawBoard = new DrawBoard();
			if(test.isVert){
				drawBoard.x = 20; 
				drawBoard.y = 900; 
				//drawBoard.init(870,570);
			}else{
				drawBoard.x = 816; 
				drawBoard.y = 302; 
				if(Datas.autoQuit){
					drawBoard.y = 296; 
				}
			}

			drawBoardBg = new Assets.signBoad_png();
			drawBoardBg.x = drawBoard.x; 
			drawBoardBg.y = drawBoard.y; 
			drawBoardBg.width = drawBoard.width;
			drawBoardBg.height = drawBoard.height;
			if(Datas.hasSignBoard)
			{
				addChild(drawBoardBg);
				addChild(drawBoard);
			}else{
				if(test.isVert){
					saveBtn.x = 350;
					rePhotoBtn.x = photoBtn.x=650;
					rePhotoBtn.y = photoBtn.y= saveBtn.y = 1330;
				}else{
					saveBtn.x = 
						rePhotoBtn.x = photoBtn.x = 1600;
					rePhotoBtn.y = photoBtn.y=400;
					saveBtn.y = 650;
				}
			}

			width = test.stageW;
			height = test.stageH;
		}

		private var rePhotoBtn:MyBtn;
		private var photoBtn:MyBtn;
		private var bichuBtn:MyBtn;
		private var resetBtn:MyBtn;

		private var saveBtn:MyBtn;
		private var backBtn:MyBtn;

		public static var drawBoard:DrawBoard;
		private var drawBoardBg:Bitmap;

		private function toClear():void
		{
			logs.adds("重签");
			hideBichu();
			drawBoard.clear();
		}
		private function toNext():void
		{
			hideBichu();
			if(photoBmp==null)
			{
				Toast.show("请点击“拍照”拍照");
				return;
			}
			if(Datas.mustSign){
				if (!drawBoard.hasDrawed)
				{
					Toast.show("请签名");
					return;
				}
			}
			UploadPage.show();
		}

		private static function delBmp(bmp:Bitmap):void
		{
			if(bmp)
			{
				if(bmp.parent)
					bmp.parent.removeChild(bmp);
				bmp.bitmapData.dispose();
			}
		}

		public static function show(clear:Boolean=true):void
		{
			test.show(main);
			hideBichu();
			if(clear){
				drawBoard.clear();
				if(photoBmp)
				{
					delBmp(photoBmp);
					photoBmp = null;
				}
				if(!Datas.autoQuit)
				{
					main.rePhotoBtn.visible = false;
				}
				main.photoBtn.visible = true;
			}
			if(MyCam.main.numChildren==0)
			{
				var _x:int = 93;
				var _y:int = drawBoard.y;
				var w:int = 643;
				var h:int = 541;
				if(test.isVert){
					_x = 10;
					_y = 270;
					w = 1060;
					h = 600;
				}else if(!Datas.autoQuit){
					_x = 22;
					_y = 208;
					w = 772;
					h = 642;
				}
				if(!Datas.hasSignBoard)
				{
					if(test.isVert){
						_x = 1080/2-w/2;
						_y = 1920/2-h/2-100;
					}else{
						_x = 1920/2-w/2;
						_y = drawBoard.y;
					}
				}
				var _bg:Shape = new Shape();
				with(_bg.graphics)
				{
					clear();
					beginFill(0xffffff,0.1);
					CONFIG::debugging{
						beginFill(0x000000,1.0);
					}
					drawRect(0,0,w,h);
					endFill();
				}
				MyCam.main.addChildAt(_bg,0);
				main.addChild(MyCam.main);
				MyCam.main.x = _x;
				MyCam.main.y = _y;
				MyCam.main.setSize(w,h);
			}
		}
		private function quit():void
		{
			NativeApplication.nativeApplication.exit();
		}

		public function toPhoto():void
		{
			rePhotoBtn.visible = false;
			if(!Datas.autoQuit)
				photoBtn.visible = false;
			if(photoBmp)
			{
				photoBtn.visible = true;
				delBmp(photoBmp);
				photoBmp = null;
				Toast.show("重拍");
				return;
			}
			if(true){
				countDown();
			}else{
				timeCounter = 0;
				showTimeRemain();
			}
		}

		private static var timeoutId:uint;
		private static var timeCounter:int;
		private static var timeTxt:TextField;
		public static var photoBmp:Bitmap;
		private function countDown():void
		{
			if(timeTxt){
				timeTxt.visible = false;
			}
			if(photoBmp)
			{
				if(photoBmp.parent)
					photoBmp.parent.removeChild(photoBmp);
				photoBmp.bitmapData.dispose();
				photoBmp = null;
			}

			timeCounter = Datas.countDown;
			CONFIG::debugging{
				timeCounter = 1;
			}
			showTimeRemain();
		}

		private function showTimeRemain():void
		{
			clearTimeout(timeoutId);
			if(timeTxt==null)
			{
				timeTxt = ViewSet.makeTxtLine("",100,0xffffff);
				timeTxt.alpha=.3;
			}
			timeTxt.text = ""+timeCounter;
			timeTxt.visible = true;
			addChild(timeTxt);
			ViewSet.fullCenter(timeTxt,MyCam.main.x,MyCam.main.y,MyCam.main.width,MyCam.main.height);

			if(timeCounter<=0)
			{
				timeTxt.visible = false;
				photoBmp = new Bitmap(WriteData.displayObject_bitmapDate(MyCam.main));
				addChild(photoBmp);
				photoBmp.x = MyCam.main.x;
				photoBmp.y = MyCam.main.y;
				Toast.show("拍照成功");
				if(Datas.autoQuit)
					photoBtn.visible = true;
				else
					rePhotoBtn.visible = true;
			}else{
				timeCounter--;
				timeoutId = setTimeout(showTimeRemain,1000);
			}
		}

	}
}

